<?php
/**
 * gcFramework
 *
 * Development framework for PHP 5 or newer
 *
 * @package		gcFramework
 * @author		Adam Martin
 * @copyright	Copyright (c) 2006
 * @link		http://www.gcFramework.com
 * @since		Version 1.0
 */

	set_error_handler('_error_handler');
	set_magic_quotes_runtime(0); 
	
/*
|-----------------------------------------|
| Load essential files                    |
|-----------------------------------------|
*/
	class_exists('gcYaml') || require_once(BASEPATH."/".FRAMEWORK."/core/gcYamlClass.php");

	
/*
|-----------------------------------------|
| Instantiate Classes                     |
|-----------------------------------------|
*/
	$gcyaml = new gcYaml;
	

/*
|-----------------------------------------|
| Load the main config (config.yml)       |
|-----------------------------------------|
*/
	if(!file_exists(BASEPATH."/".APP.'/config/'.CONFIGFILE.'.yml')){
		displayError("Config File does not exist");
	} else {
		$config	= $gcyaml->loadconfig(CONFIGFILE);
	}
	if(!isset($config) OR !is_array($config)){
		displayError("Config file is invalid");
	}


/*
|-----------------------------------------|
| Common definitions                      |
|-----------------------------------------|
*/

	define('LANGUAGE', $config[MODE]["language"]); 
	define('TIMEZONE', $config[MODE]["timezone"]);
	define('LIVESITE', $config[MODE]["baseurl"]);
	
	define('DBHOST', $config[MODE]["dbhost"]);
	define('DBUSER', $config[MODE]["dbuser"]);
	define('DBPASS', $config[MODE]["dbpass"]);


/*
|-----------------------------------------|
| Load required files                     |
|-----------------------------------------|
*/

require_once(BASEPATH."/".FRAMEWORK."/i18n/".LANGUAGE."/lang.php");
require_once(BASEPATH."/".APP."/i18n/".LANGUAGE."/lang.php");

$helpersdir = opendir(BASEPATH."/".FRAMEWORK."/helpers/");
while (false !== ($helper = readdir($helpersdir))){
	$ext = substr(strrchr($helper, "."), 1);;
	if($ext=="php" && substr($helper, 0, 9)=="gcHelper_"){
		require_once(BASEPATH."/".FRAMEWORK."/helpers/".$helper);	
	}	
}


/*
|-----------------------------------------|
| Error Handler and Logger                |
|-----------------------------------------|
*/

	function _error_handler($logtype, $logmsg, $logfile, $logline){ 
		global $gcyaml;
		$logdate = gmdate('d-m-y');
		$logtime = gmdate('H:i:s');
		$array[] = array('logdate' => $logdate, 'logtime' => $logtime, 'logtype' => $logtype,'logmsg' => trim($logmsg), 'logfile' => trim($logfile), 'logline' => $logline);
		$gcyaml->logger($array, "main"); //@todo GC: logic to write to correct file (error, warning, info etc)
	}


/*
|-----------------------------------------|
| Common Functions                        |
|-----------------------------------------|
*/
	

	function displayError($errmessage){ //@todo: error template / error display at top of page
		echo ("ERROR: ".$errmessage);
		exit; 
	}
	
	//parse template
	function loadtpl($tpl='default', $style=''){
		if(!$style || $style=="" || $style==null){
			$style = $tpl;	
		}
		
		//stylesheet
		$stylesheet = "<link rel='stylesheet' type='text/css' href='".LIVESITE."/".APP."/css/".$style.".css' />";
		//ie stylesheet
		if(file_exists(BASEPATH."/".APP."/css/".$style."-ie.css")){
			$stylesheet .="<!--[if lt IE 7]>\n";
			$stylesheet .="<link rel='stylesheet' type='text/css' href='css/".$style."-ie.css' media='screen' />\n";
			$stylesheet .="<![endif]-->\n";
		}
		$stylesheet .="\n</head>\n";
		
		
		$extras = "<link rel='Shortcut Icon' type='image/ico' href='media/images/favicon.ico' />\n";
		
		//js files
		$jsdir = opendir(BASEPATH."/".APP."/scripts/");
		while (false !== ($js = readdir($jsdir))){
			$ext = substr(strrchr($js	, "."), 1);;
			if($ext=="js"){
				echo "<script type='text/javascript' src='scripts/".$js."'></script>\n";	
			}	
		}
		
		//load template
		$filename = BASEPATH."/".APP."/views/".$tpl.".tpl";
		$handle = fopen($filename, "r");
		$contents = fread($handle, filesize($filename));
		fclose($handle);
		
		//replace variables
		$contents = preg_replace_callback('/\{([^}].+?)\}/', 'replace_vars', $contents);
		
		//replace language elements
		$contents = preg_replace_callback('/\[lang:([^}].+?)\]/', 'replace_lang', $contents);
		
		echo $extras;
		echo $stylesheet;
		echo $contents;
	}
	//this is used by loadtpl only.
	function replace_vars($matches){
		global $data;
		return $data[$matches[1]];	
	}
	
	//this is used by loadtpl only.
	function replace_lang($matches){
   		return eval("return $matches[1];");		
	}
	

?>
